<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" title="工作流模型新增" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="160px">
      <el-form-item label="流程名称" prop="busMainData.flowName">
        <el-input v-model="form.busMainData.flowName" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="流程Key" prop="busMainData.flowKey">
        <el-input v-model="form.busMainData.flowKey" style="width: 300px;"/>
      </el-form-item>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','WORKFLOW_DEF_ALL','WORKFLOW_DEF_CREATE']" :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrEdit, toAdd } from '@/api/system/workflowDef'

export default {
  components: { },
  props: {
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      isAdd: true,
      loading: false,
      dialog: false,
      form: {
        attachmentList: [],
        busMainData: {
          id: '',
          flowState: '0', // 未部署
          flowVersion: 0
        },
        busPageInfo: {
        },
        busCommonState: {
          selfState: '10' // 默认已生效
        }
      },
      rules: {
        'busMainData.flowName': [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ],
        'busMainData.flowKey': [
          { required: true, message: '请输入流程key', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: { }}
      toAdd(data).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>
